<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>采购询价单</title>
<link rel="stylesheet" type="text/css"
	href="../extjs/resources/css/ext-all.css" />
<script type="text/javascript" src="../extjs/ext-all.js"></script>
<script type="text/javascript" src="../extjs/ext-lang-zh_CN.js"></script>
	
<script>
//供应商id
var gongyignshang; 

Ext.onReady(function(){
	 //Ext.BLANK_IMAGE_URL = "extjs/resources/images/default/s.gif";
	 // Ext.QuickTips.init();
	
	 //加载第一页的信息
	 querybypage(1);
	 windowst.show();
 
});
 
 var grids3;
//查询
var fielmenu1=new Ext.menu.Menu({  
		items:[{text:'历史交易查询',listeners:{  
			 'click':function(){
				 queryhistory();
				 creategrid2();
				 Ext.create('Ext.window.Window',{  
		    		   id:'myWindow',             
		    		   title: '历史交易查询',  
		    		   width:550,
		    		   height:350,
		    		   bodyPadding: 15,
		    		   items:[{
			  		 		 style:"margin-left:30px;",
					 		 xtype: 'textfield',
					         name: 'customerid',
					         fieldLabel: '供应商',
					         id:'customerids', 
					 		 width:250,
					         labelWidth: 80,
					         anchor: '100%',
		    		   },{
		    			   style:"float:right;margin-top:-27px;margin-right:56px;",
		    			   width:50,
		    			   xtype: 'button', text: '取回',
		    				   listeners: { "click": function (){       
		    		               if(grids2.getSelectionModel().getCount()==0){
		    		                	 Ext.Msg.alert("提示信息", "请选中一行数据!");  
		    		                	 return;
		    		                }else
		    		                {              
		    		                	 var rows = grids2.getView().getSelectionModel().getSelection();      
		    		                     grids.getStore().add(rows[0].data);
		    		                     Ext.getCmp('myWindow').close();                                                          
		    		                 }
		    		              }}
				 		},grids2]
		    	  }).show(); 
				 Ext.getCmp("customerids").setValue(Ext.getCmp("customerid").getValue());
			 } 	
		}}]
	});
	
	//历史交易记录的grid
    var grids2; 
    
	function creategrid2(){
	 	grids2=Ext.create('Ext.grid.Panel',{
		store:Store,
		id:"his", 
		columns: [
	        			 { header: '单别',  dataIndex: 'trantype'},
	        			 { header: '日期',  dataIndex: 'billdate'},
	        			 { header: '单号',  dataIndex: 'fromno'},
	        			 { header: '币别',  dataIndex: 'currid'},
	        			 { header: '汇率',  dataIndex: 'exchrate'},
	        		     { header: '物料编号',  dataIndex: 'prodId'},
	        		     { header: '物料名称', dataIndex: 'prodName'},
	        		     { header: '规格型号', dataIndex: 'prodsize'},
	        		     { header: '单位', dataIndex: 'danwei'},
						 { header: '数量', dataIndex: 'squantity'},
						 { header: '折扣前单价', dataIndex: 'oldprice'},
						 { header: '折数(%)', dataIndex: 'discount'},
						 { header: '单价', dataIndex: 'sPrice'},
						 { header: '金额', dataIndex: 'amount'},
						 { header: '单价是否含税', dataIndex: 'priceoftax'},
	   					 ],
	   			 width:500,
			     height:250,
				 autoScroll:false,	
	})
}
var fielmenua=new Ext.menu.Menu({
		items:[{text:'销售订单转入',listeners:{  
			 'click':function(){
				alert(231);
			 }
		 }},{text:'采购请购单转入',listeners:{  
			 'click':function(){
				
			 }
		 }}]
});	

//功能	
var fielmenu2=new Ext.menu.Menu({
		items:[{text:'批次变更单价'}]
	});
//操作
var fielmenu3=new Ext.menu.Menu({
		items:[{text:'上一页',listeners:{  
			 'click':function(){
				//grids.store.removeAll()//清空之前的数据
				if(pb.pagenum==1){
	    			Ext.Msg.alert('提示','已经是第一页了!');
	    			return;
	    		}
				else{
					querybypage(pb.previouspage);
	    		}
			 }
		}},{text:'下一页',listeners:{  
			 'click':function(){
				 //grids.store.removeAll()//清空之前的数据
					if(pb.pagenum==pb.maxpage){
		    			Ext.Msg.alert('提示','已经是最后一页了!');
		    			return;
		    		}
					else{			
						querybypage(pb.nextpage);
				}}
		}},{text:'新增/修改',listeners:{  
			 'click':function(){
				addxj();
			 }
		}},{text:'删除',listeners:{  
			 'click':function(){	
				deletexj();
			}
		}},{text:'审核',listeners:{  
			 'click':function(){
				updateshzt();
			 }
		}},{text:'反审核',listeners:{  
			 'click':function(){
				updateshzt1();
			 }
		}}]
});

var grids=Ext.create('Ext.grid.Panel',{
	 id:"gir",
	 store:grids3,
	 listeners:{
				containerdblclick: function(grid, e, eOpts){ //单击事件
	    						grid.getStore().add({'prodId':'', 'prodName':'','prodsize':''});		
    				}
			},plugins: [
        				Ext.create('Ext.grid.plugin.CellEditing', {
            				clicksToEdit: 1
        				})
    				],
		columns:[
				 {header: '（栏号）',
	                    		xtype: 'rownumberer',
	                    		dataIndex: 'serno', 
	                    		width: 80,
								sortable: false
        					},
       			 { header: '物料编号', dataIndex: 'prodId',width: 120,editor: {xtype:'textfield',
				 	listeners:{
				 		specialkey:function(grid,e,eOpts){
				 			//按enter键弹出窗口
			                if (e.getKey()==Ext.EventObject.ENTER){               	 
								querywl();             	
			                }
			            }	
				 	}}},
				 	 { header: '物料名称', dataIndex: 'prodName',width:120},
				 	 { header: '规格型号', dataIndex: 'prodsize',width:120},
					 { header: '折扣前单价', dataIndex: 'oldprice',width: 120 },
					 { header: '折数(%)', dataIndex: 'discount' ,width: 120},
					 { header: '单价', dataIndex: 'sPrice' ,width: 120},
					 { header: '数量', dataIndex: 'squantity' ,width: 120},
					 { header: '金额', dataIndex: 'amount' ,width: 120},
					 { header: '税率', dataIndex: 'taxrate',width: 120 },
					 { header: '税额', dataIndex: 'taxamt' ,width: 120},
					 { header: '含税金额', dataIndex: 'Vftotal' ,width: 120},
					 { header: '来源单别', dataIndex: 'trantype',width: 120 },
					 { header: '来源单号', dataIndex: 'fromno',width: 120 },
					 { header: '赠品', dataIndex: 'isgift' ,width: 120}
   					 ],
   			 height: 130,
			 autoScroll:false,
			 
})
var store1 = Ext.data.JsonStore({
		fields : []	
	});
var store2 = Ext.data.JsonStore({
	fields : []	
});	

//供应商加载grid并带出采购人员以及部门
function querygys(){
	var grids3=Ext.create('Ext.grid.Panel',{   
		columns: [
					 {header: '（栏号）',
		                    		xtype: 'rownumberer',
		                    		dataIndex: 'serno', 
		                    		width: 100,
									sortable: false
	        					},
	        			 { header: '供应商编号',  dataIndex: 'gysid', width: 120},
	        			 { header: '供应商名称',  dataIndex: 'gysname', width: 120}	
	   					 ],
	   			 store:store1,
	   			 height: 230,
				 width:390,
				 autoScroll:false,				 
	})
//点击取回按钮	
var btn=Ext.create("Ext.Button", {
	style:"margin:6px 280px;",  
    width:60,
	text: "取回",
    listeners: { "click": function () {         
       if(grids3.getSelectionModel().getCount()==0){
        	 Ext.Msg.alert("提示信息", "请选择一行数据!");  
        	 return;
        }else{
        	 var selectedData=grids3.getSelectionModel().getSelection()[0].data; 
        	 gongyingshang=selectedData.gysid;
        	 Ext.Ajax.request({ 
					url : "/zjj/querygysbyid",
					params : {GYSid:selectedData.gysid},
					method : "post",
					success : function(data){
						var la=Ext.JSON.decode(data.responseText);
						
						Ext.getCmp('customerid').setValue(la.gysname);					
						Ext.getCmp('salesid').setValue(la.personnelname);
						Ext.getCmp('departid').setValue(la.deptname);
						win.close();  
					},
					failure : function() {
						
					}				 		 
				 });     
             }
        }
    },
    id: "btn",
});	
//弹出供应商窗口	
var win=Ext.create('Ext.window.Window', {  
            id:'khWindow',             
            title: '供应商信息',  
            width:400,
			height:300,  
			items:[btn,grids3],
			closeAction: 'destroy',
			 listeners: {
				 activate : function() {							    	      
					 Ext.Ajax.request({ 
							url : "/zjj/querygys",
							method : "post",
							success : function(data) {
								var me=Ext.JSON.decode(data.responseText);
								grids3.getStore().loadData(me);
							},
							failure : function() {
								
							}
				 		 
						  }); 	 						 
		    		 }
	     		 } 	  					   
       	 }).show();     		
}

	//查询物料并加载到grid中
    function querywl(){
    		var grids4=Ext.create('Ext.grid.Panel',{   
        		columns: [
        					 {header: '（栏号）',
        		                    		xtype: 'rownumberer',
        		                    		dataIndex: 'serno', 
        		                    		width:100,
        									sortable: false
        	        					},
        	        			 { header: '物料编号',  dataIndex: 'prodId', width: 120},
        	        			 { header: '物料名称',  dataIndex: 'prodName', width: 120}	
        	   					 ],	   			 
        	   			 store:store2,
        	   			 height: 230,
        				 width:390,
        				 autoScroll:false,				 
    })
    
    //物料窗体的取回按钮
    var btn3=Ext.create("Ext.Button", {
        style:"margin:6px 280px;",  
        width:60,
    	text: "取回",
            listeners: { "click": function (){             
               if(grids4.getSelectionModel().getCount()==0){
                	 Ext.Msg.alert("提示信息", "请选中一行!");  
                	 return;
                }else{
                	 var selectedData=grids4.getSelectionModel().getSelection()[0].data;
                	 Ext.Ajax.request({ 
							url : "/zjj/querywlbyid",
							params : {prodId:selectedData.prodId},
							method : "post",
							success : function(data) {
								var na=Ext.JSON.decode(data.responseText);	
							    grids.getSelectionModel().getSelection()[0].set("prodId",na[0].prodId);  
							    grids.getSelectionModel().getSelection()[0].set("prodName",na[0].prodName); 
							    grids.getSelectionModel().getSelection()[0].set("prodsize",na[0].prodsize); 
								win2.close();
							},
							failure : function()
							{
								
							}
						 });     
                     }
                }
            },
            id: "btn3",
        });	
    
	//物料信息的窗口	
	var win2=Ext.create('Ext.window.Window', {  
                id:'wlWindow',             
                title: '选择物料',  
                width:400,
				height:300,  
				items:[btn3,grids4],
				closeAction: 'destroy',
				 listeners: {
					 activate : function() {							    	      
						 Ext.Ajax.request({ 
								url : "/zjj/querywl",
								method : "post",
								success : function(data) {
									var mm=Ext.JSON.decode(data.responseText);
									grids4.getStore().loadData(mm);
								}
						  }); 	 						 
			     }
		      } 	  					   
          }).show();     		
	}	    

var filterPanel = Ext.create('Ext.panel.Panel', {
    bodyPadding: 6,  // 避免Panel中的子元素紧邻边框
    width: 1985,
	height:170,
    items:[grids],
	bbar:[{xtype:"label",text:'总计:',width:90},
	{xtype:"textfield",disabled:true,width:90,margin:'0 0 0 320',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0 0 0 14',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0 0 0 78',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0 0 0 14',value:'0.00'}],
});

var tableds=Ext.create('Ext.TabPanel',{
	width:"100%",
	height:230,
	autoScroll:true, 
	bodyPadding: 5,
	items:[{title:'内容',autoScroll:true,items:[filterPanel]},
	       {title:'备注',xtype:'textarea',name:'Remark',fieldLabel: '备注',labelWidth: 30,}],
});


var formsg=Ext.create('Ext.form.Panel', {
    bodyPadding: 5,
	height:'100%',
	layout:"column",
	baseCls:'x-plain',
	items:[{
		xtype: 'textfield',
        name: 'salesid',
        id:'salesid',
        fieldLabel: '采购人员',
        style:'margin-left:18px;color:blue',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        name: 'maker',
        id:'maker',
        fieldLabel: '制单人员',
		style:'margin-left:40px',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
		xtype: 'textfield',
        name: 'departid',
        id:'departid',
        style:'margin-left:18px;color:blue',
        fieldLabel: '所属部门',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        name: 'permitter',
        id:'permitter',
        fieldLabel: '复核人员',
		style:'margin-left:40px',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			}],
	});

var forms=Ext.create('Ext.form.Panel', {
    bodyPadding: 5,
	height:'100%',
	layout:"column",
	baseCls:'x-plain',
	items:[
	  {
		style:"margin-left:18px;color:blue",
		xtype: 'textfield',
        name: 'customerid',
        fieldLabel: '供应商',
        id:'customerid',
		width:250,
        labelWidth: 80,
        anchor: '100%',
        listeners:{
            specialkey:function(field,e){
                if (e.getKey()==Ext.EventObject.ENTER){               	 
                	querygys();               	
                }
            }
        }},
	   {
		style:'margin-left:40px;color:blue',
		xtype: 'datefield',
        name: 'billdate',
        fieldLabel: '单据日期',
        id:'billdate',
        format:'Y-m-d',
		width:250,
        labelWidth: 70,
        anchor: '100%',
        listeners:{
           blur: function()
           {
               //失去焦点事件
              	alert(Ext.getCmp('billno').getValue());
               //alert(Ext.getCmp('billdate').getValue());
              // Ext.getCmp('billno').getValue()=Ext.getCmp('billdate').getValue()+"01";    
           }
          }
		},{
		style:'margin-left:18px',
		xtype: 'textfield',
        name: 'outaddress',
        fieldLabel: '送货地址',
        id:'outaddress',
		width:150,
        labelWidth: 80,
        anchor: '100%', 
			},{
		xtype: 'textfield',
        name: 'dizhiton',
		width:99,
        labelWidth: 70,
		margin:"0 1",
        anchor: '100%',
			},{
		xtype: 'textfield',
        name: 'billno',
        id:'billno',
		style:"color:blue;margin-left:40px",
        fieldLabel: '单据号码',
		width:250,
        labelWidth: 70,
        anchor: '100%',
			},{
		style:'margin-left:18px;color:blue',
		xtype: 'datefield',
        name: 'validDate',
        id:'validDate',
        fieldLabel: '有效日期',		
		width:250,
		format:'Y-m-d',
        labelWidth: 80,
        anchor: '100%',
        listeners:{
            blur: function()
            {
                //失去焦点事件
                //alert(Ext.getCmp('validDate').getValue()); 
             }
           }
		},{
		style:'margin-left:40px',
		xtype: 'textfield', //多行文本域
        name: 'currid',
        id:'currid',
        fieldLabel: '币别',
        labelWidth: 70,
		width:250,
        anchor: '100%',
			},{
				    style:'margin-left:18px',
	             	fieldLabel: '单价是否含税',
					xtype:'combo',
	             	name: 'priceoftax',
	             	id:'priceoftax',
	             	displayField: 'name',
					labelWidth:80,
					width:250,
    				valueField: 'abbr',
					store: Ext.create('Ext.data.Store', {
						fields: ['abbr', 'name'],
    					data : [
        					{"abbr":"含税", "name":"含税"},
        					{"abbr":"未税", "name":"未税"}
							
					   ]
					}),
					value: '含税',
        			typeAhead: true,
        			mode: 'local',
        			triggerAction: 'all',
        			selectOnFocus:true,
        			
	         	},{
		style:'margin-left:40px',
		xtype: 'textfield', //多行文本域
        name: 'exchrate',
        id:'exchrate',
        fieldLabel: '汇率',
        labelWidth: 70,
		width:250,
        anchor: '100%',
			},tableds,formsg
	]
});
//winform窗口
	var windowst=new Ext.Window({
		width:638,
		height:480,
		title:"采购询价单",
		closable:true,
		resizable:false, //设置是否可以改变大小
		draggable:true,
		anchor: '100%',
		items:[forms],
		bbar:[{xtype:"splitbutton",text:'查询',width:90,menu:fielmenu1},
		     {xtype:"splitbutton",text:'转单',width:90,menu:fielmenua},
			 {xtype:"splitbutton",text:'功能',width:90,menu:fielmenu2},
		     {xtype:"splitbutton",text:'操作',width:90,menu:fielmenu3}],	  
		});
	
	
	//定义分页的全局变量pb
	var pb = null;
	
	//封装查询分页方法
	function querybypage(num)
	{
		Ext.Ajax.request({  
	        //被用来向服务器发起请求默认的url  
	        url : "/zjj/querybypage",  
	        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串  
	        params :{pagenum:num},  
	        //请求时使用的默认的http方法  
	        method : "post",  
	        //请求成功时回调函数  
	        success : function(data) {  
	        	pb=Ext.JSON.decode(data.responseText);
	            alert(data.responseText);
	            gongyingshang=pb.pagedata[0].customerid;
	        	Ext.getCmp('billno').setValue(pb.pagedata[0].billno);
				Ext.getCmp('customerid').setValue(pb.pagedata[0].customername);
				Ext.getCmp('billdate').setValue(pb.pagedata[0].billdate);
				Ext.getCmp('validDate').setValue(pb.pagedata[0].validDate);
				Ext.getCmp('currid').setValue(pb.pagedata[0].currid);
				Ext.getCmp('exchrate').setValue(pb.pagedata[0].exchrate);
				Ext.getCmp('outaddress').setValue(pb.pagedata[0].outaddress);
				Ext.getCmp('priceoftax').setValue(pb.pagedata[0].priceoftax);
				Ext.getCmp('maker').setValue(pb.pagedata[0].maker);
				Ext.getCmp('salesid').setValue(pb.pagedata[0].salesid);
				Ext.getCmp('permitter').setValue(pb.pagedata[0].permitter);
				Ext.getCmp('departid').setValue(pb.pagedata[0].departid);

				grids.getStore().loadData(pb.pagedata[0].list);
				
	        },
	        //请求失败时回调函数  
	        failure : function() {  
	        	Ext.Msg.alert("信息提示", "数据不正确!!");  
	        }  
		});  	
	}
	
	var Store; //自定义数据集
    var data; //数据   
    
	//封装查询询价表历史交易记录的方法
	function queryhistory()
	{             
    	Ext.Ajax.request({
			    url: "/zjj/queryhistory?customerid="+gongyingshang+"",  
			    async:false,
			    success: function(response){
			    data=response.responseText;
			    var obj = JSON.parse(data);
	            Store =Ext.create('Ext.data.Store',{
					 storeId:'simpsonsStore',
					 fields:['billdate','currid','exchrate','prodId','danwei','oldprice','discount','sPrice','squantity','amount','priceoftax','prodName','prodsize','trantype','fromno'],
					 data:{'items':obj},
					 proxy: {
						type: 'memory',
						reader: {
							type: 'json',
							root: 'items'
						}
					 }
				});       
			  }
	     });  
	}
	
	 
	//封装新增方法
	function addxj()
	{
		var bc={};
		bc['billno'] = Ext.getCmp('billno').getValue();
		bc['customerid'] = gongyingshang;
		bc['billdate'] = Ext.getCmp('billdate').getValue();
		bc['validDate'] = Ext.getCmp('validDate').getValue();
		//bc['Shifoushenhe'] = Ext.getCmp('Shifoushenhe').getValue();
		bc['priceoftax'] = Ext.getCmp('priceoftax').getValue();
		//bc['Sumtotal'] = Ext.getCmp('Sumtotal').getValue();
		bc['currid'] = Ext.getCmp('currid').getValue();
		bc['exchrate'] = Ext.getCmp('exchrate').getValue();
		bc['outaddress'] = Ext.getCmp('outaddress').getValue();
		bc['departid'] = Ext.getCmp('departid').getValue();
		bc['maker'] = Ext.getCmp('maker').getValue();
		bc['salesid'] = Ext.getCmp('salesid').getValue();
		bc['permitter'] = Ext.getCmp('permitter').getValue();
		
		//详表
		bc['list'] = Ext.pluck(grids.getStore().data.items,'data');
		
		alert(JSON.stringify(bc));
		
		Ext.Ajax.request({  
	        //被用来向服务器发起请求默认的url  
	        url : "/zjj/addxj",
	        //请求时发送后台的参数,既可以是Json对象，也可以直接使用“name = value”形式的字符串 
	       	headers: {'Content-Type':'application/json'},
	        params :JSON.stringify(bc),  
	        //请求时使用的默认的http方法  
	        method : "post",
	        //请求成功时回调函数  
	        success : function(){
	        	Ext.Msg.alert("信息提示","新增成功!!!");      
	        },
	        failure:function(){
	        	Ext.Msg.alert("信息提示","新增失败");
	        }
		});  
	}
	
	//封装删除方法
	function deletexj()
	{
		alert(Ext.getCmp('billno').getValue());
		Ext.Ajax.request({   
              url:"/zjj/deletexj?billno="+Ext.getCmp('billno').getValue()+"", 
              success:function(){   
              Ext.Msg.alert("信息提示","您已经成功删除该信息！");      
              },   
              failure:function(){   
                 Ext.Msg.alert('错误','删除失败！！！');   
               }   
             });              
             Ext.Msg.alert('删除操作','请选择一行你要删除的数据！');     
	}
	
	//封装审核方法
	function updateshzt()
	{
		Ext.Ajax.request({   
              url:"/zjj/updateshzt?id="+Ext.getCmp('billno').getValue()+"",
              success:function(){   
              	Ext.Msg.alert("信息提示","审核成功！");      
              },   
              failure:function(){   
                 Ext.Msg.alert('错误','审核失败！');   
               }   
            });              
	}
	
	//封装反审核方法
	function updateshzt1()
	{
		Ext.Ajax.request({
              url:"/zjj/updateshzt1?id="+Ext.getCmp('billno').getValue()+"",  
              success:function(){   
              	Ext.Msg.alert("信息提示","反审核成功！");
              },   
              failure:function(){   
                 Ext.Msg.alert('错误','反审核失败！');   
               }   
           });              
	}
</script>
</head>
<body>
</body>

</html>